//
//  HDownloadExamViewController.m
//  NursingManages
//
//  Created by Zhen Huang on 17/10/12.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "HDownloadExamViewController.h"
#import "DIHTest.h"
#import "HttpDownload.h"
#import "HDowloadDataViewController.h"
#import <TMCache.h>

@interface HDownloadExamViewController ()
<UITableViewDelegate,
UITableViewDataSource>

@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong)NSMutableArray *dataArray;
@property (assign, nonatomic) NSIndexPath *selIndex;//单选，当前选中的行
@property (nonatomic,strong)NSMutableArray *selectorPatnArray;//存放选中数据


@end

@implementation HDownloadExamViewController

- (instancetype)init {
    
    if (self) {
        
        self.dataArray = [NSMutableArray new];
        self.selectorPatnArray = [NSMutableArray new];
        NSString *url = AppServiceConfig.GetServiceUrl;
        self.downUrl  = [NSString stringWithFormat:@"%@/%@?token=%@",url,@"GetExamData",[[TMCache sharedCache] objectForKey:@"deviceToken"]];
    }
    return self;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self getData];

    [self.view addSubview:self.tableview];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *execbtn = [[UIBarButtonItem alloc] initWithTitle:@"下载" style:UIBarButtonItemStylePlain target:self action:@selector(didUploadButttonClicked:)];
    self.navigationItem.rightBarButtonItem = execbtn;
    // Do any additional setup after loading the view.
}

-(UITableView *)tableview
{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, NavBarHeight, ScreenWidth, ScreenHeight - NavBarHeight) style:UITableViewStylePlain];
        _tableview.dataSource = self;
        _tableview.delegate = self;
        _tableview.tableFooterView = [[UIView alloc]init];
        [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        
    }
    return _tableview;
}

- (void)getData {
    
    [HCommon popupMessage:@"正在加载"];
    
    [Networking receiveDataWithURLString:self.downUrl method:@"GET" body:nil Block:^(NSData *tempData) {
        
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:tempData options:NSJSONReadingMutableContainers error:nil];
        if ([[dic objectForKey:@"flag"]integerValue] == 1) {
            
            [HCommon dismissAnimated:YES];
            
            HDowloadDataViewController *HDownLoadVC = [[HDowloadDataViewController alloc]init];
            [HDownLoadVC downLoadExam];
            NSDictionary *data = [dic objectForKey:@"data"];
            NSArray *array = [data objectForKey:@"data_quesbank_type"];
            for (NSDictionary *modelDic in array) {
                Model_Hr_Quesbank_Type *model  = [[Model_Hr_Quesbank_Type alloc]initWithDictionary:modelDic];
                
                if ([model.PARENT_ID isEqualToString:@"*"]) {
                    [self.dataArray addObject:model];
                }
                
                [self.tableview reloadData];

            }
        }else {
            if ([dic allKeys].count == 0) {
                [HCommon popupErrorMessage:@"获取试题列表失败"];
            }else {
                [self alert:[dic objectForKey:@"msg"]];
            }
        }
    }];
     
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (_selIndex == indexPath) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    Model_Hr_Quesbank_Type *model = self.dataArray[indexPath.row];
    cell.textLabel.text = model.BANKNAME;
    [self.tableview setEditing:YES animated:YES];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [self.selectorPatnArray addObject:self.dataArray[indexPath.row]];

}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}



- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    //从选中中取消
    if (self.selectorPatnArray.count > 0) {
        
        [self.selectorPatnArray removeObject:self.dataArray[indexPath.row]];
    }
    
}
- (void) didUploadButttonClicked:(UIButton *)sender{
    
    DIHTest *test = [[DIHTest alloc]init];
    [test startDownloadQuesWithid:self.selectorPatnArray downLoad:^{
        
    }];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
